package org.pp.controller;

import cn.hutool.core.util.IdUtil;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.pp.config.MiniappConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay/mini")
@Slf4j
public class PayMiniappController {

    @Autowired
    MiniappConfig miniappConfig;

    @Resource
    RSAAutoCertificateConfig config;

    @GetMapping("/prepay")
    @Operation(summary = "测试小程序预支付")
    public Object prepay(){
        JsapiServiceExtension jsapi =
                new JsapiServiceExtension.Builder().config(config).build();
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        //1分钱
        amount.setTotal(1);
        request.setAmount(amount);
        request.setAppid(miniappConfig.getAppid());
        request.setMchid(miniappConfig.getMerchantId());
        request.setDescription("商品:测试");
        request.setNotifyUrl(miniappConfig.getNotify());
        request.setOutTradeNo(IdUtil.fastSimpleUUID());
        Payer payer = new Payer();
        payer.setOpenid("o5M2v4vabWZmZkqjbn-ORScZ2w9E");
        request.setPayer(payer);
        PrepayWithRequestPaymentResponse prepayWithRequestPaymentResponse =
                jsapi.prepayWithRequestPayment(request);


        return prepayWithRequestPaymentResponse;
        //return BeanUtil.copyProperties(prepayWithRequestPaymentResponse, PayResponse.class);
    }

}
